const db = uniCloud.database()
const utils = require('my-utils')
module.exports = {
	_before: async function() { // 通用预处理器
		this.userInfo = await utils.getUserInfo(this)
	},
	async getAll() {
		if (!this.userInfo.uid) return this.userInfo
		return await db.collection('my-mall-categories').get()
	},

	async update(data) {
		if (!this.userInfo.uid) return this.userInfo
		const newData = {
			...data
		}
		delete newData._id
		return db.collection('my-mall-categories').doc(data._id).update(newData)
	},

	async add(data) {
		if (!this.userInfo.uid) return this.userInfo
		return await db.collection('my-mall-categories').add(data)
	},

	async delete(id) {
		if (!this.userInfo.uid) return this.userInfo
		return db.collection('my-mall-categories').doc(id).remove()
	}
}